package com.example.rentingapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.HouseEntity;
import com.example.rentingapp.enetity.HouseResponse;
import com.example.rentingapp.enetity.InformationEntity;
import com.example.rentingapp.enetity.InformationHouseEntity;
import com.example.rentingapp.enetity.InformationHouseResponse;
import com.example.rentingapp.enetity.InformationResponse;
import com.example.rentingapp.enetity.OrdersEntity;
import com.example.rentingapp.enetity.OrdersResponse;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NewHouseInformationActivity extends BaseActivity implements View.OnClickListener{
    private  EditText  et_location, et_address, et_homeType, et_timeType, et_houseRent, et_renovation, et_other, et_area;
    private  String  location, address, homeType, timeType, houseRent, renovation, other, area;
    private Button publish_btn,back_btn;
    private List<HouseEntity> datas = new ArrayList<>();
    private List<InformationHouseEntity> datas2 =new ArrayList<>();
    String house_id;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    et_location.setText(datas2.get(0).getLocation());
                    et_address.setText(datas2.get(0).getAddress());
                    et_homeType.setText(datas2.get(0).getHome_type());
                    et_timeType.setText(datas2.get(0).getTime_type());
                    et_houseRent.setText(String.valueOf(datas2.get(0).getHouse_rent()));
                    et_renovation.setText(datas2.get(0).getRenovation());
                    et_area.setText(String.valueOf(datas2.get(0).getArea()));
                    et_other.setText(datas2.get(0).getOther());
                    break;
                case 1:
                    et_location.setText(datas.get(0).getLocation());
                    et_address.setText(datas.get(0).getAddress());
                    et_homeType.setText(datas.get(0).getHome_type());
                    et_timeType.setText(datas.get(0).getTime_type());
                    et_houseRent.setText(String.valueOf(datas.get(0).getHouse_rent()));
                    et_renovation.setText(datas.get(0).getRenovation());
                    et_area.setText(String.valueOf(datas.get(0).getArea()));
                    et_other.setText(datas.get(0).getOther());
                    showToast("房源信息修改成功");
                    break;
            }
        }
    };
    @Override
    protected int initLayout() {
        return R.layout.activity_new_house_information;
    }

    @Override
    protected void initView() {
        publish_btn=findViewById(R.id.btn_publish);
        back_btn=findViewById(R.id.btn_back);
        et_location = findViewById(R.id.editText_location2);
        et_address = findViewById(R.id.editText_address2);
        et_homeType = findViewById(R.id.editText_home_type2);
        et_timeType = findViewById(R.id.editText_time_type2);
        et_houseRent= findViewById(R.id.editText_house_rent2);
        et_renovation = findViewById(R.id.editText_renovation2);
        et_other = findViewById(R.id.editText_other2);
        et_area = findViewById(R.id.editText_area2);
    }

    @Override
    protected void initData() {
        house_id = findByKey("house_id");
        search(house_id);
        publish_btn.setOnClickListener(this);
        back_btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_publish:
                location=et_location.getText().toString().trim();
                address=et_address.getText().toString().trim();
                homeType=et_homeType.getText().toString().trim();
                timeType=et_timeType.getText().toString().trim();
                houseRent=et_houseRent.getText().toString().trim();
                renovation=et_renovation.getText().toString().trim();
                other=et_other.getText().toString().trim();
                area=et_area.getText().toString().trim();
                change();
                navigateTo(HousingDetailsActivity2.class);
                finish();
                break;
            case R.id.btn_back:
                navigateTo(HousingDetailsActivity2.class);
                finish();
                break;
        }
    }


    //修改房源信息
    private void change() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("house_id", house_id);
        params.put("location", location);
        params.put("address", address);
        params.put("home_type", homeType);
        params.put("time_type", timeType);
        params.put("house_rent", houseRent);
        params.put("renovation", renovation);
        params.put("other", other);
        params.put("area", area);
        Api.config(ApiConfig.CHANGE_HOUSE_BY_HOUSE_ID, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                HouseResponse response = gson.fromJson(res, HouseResponse.class);
                if (response.getSuccess() == true) {
                    List<HouseEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(1);
                    }
                } else {
                    showToastSync("房源信息完善失败");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }

    private void search(String house_id){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("house_id",house_id);//从本地获取house_id
        Api.config(ApiConfig.FIND_INFORMATION_HOUSE, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationHouseResponse response = gson.fromJson(res, InformationHouseResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationHouseEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas2 = list;
                        mHandler.sendEmptyMessage(0);
                    }
                } else {
                    showToastSync("房源信息未完善");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}
